package br.com.ozsolucoes.sessionbeans;

import java.io.Serializable;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import br.com.ozsolucoes.entity.PessoaFisica;

@Stateless
public class PessoaFisicaSessionBean implements Serializable {

	private static final long serialVersionUID = -4582260796751299125L;

	@PersistenceContext
	private EntityManager entityManager;

	public PessoaFisica consultarPorId(long idPf) {
		Query query = entityManager.createNamedQuery("PF.consultarPorId");
		query.setParameter("idPf", idPf);
		try {
			return (PessoaFisica) query.getSingleResult();
		} catch (NoResultException e) {
			return null;
		}
	}
	public long consultarIdPorNome(String nomecliente) {
		Query query = entityManager.createNamedQuery("PF.consultarIdPorNome");
		query.setParameter("nomecliente", nomecliente.toUpperCase());
		try {
			return (Long) query.getSingleResult();
		} catch (NoResultException e) {
			return 0;
		}
	}
	public long consultarIdPorCpf(String cpf) {
		Query query = entityManager.createNamedQuery("PF.consultarIdPorCpf");
		query.setParameter("cpf", cpf);
		try {
			return (Long) query.getSingleResult();
		} catch (NoResultException e) {
			return 0;
		}
	}

	@SuppressWarnings("unchecked")
	public List<PessoaFisica> consultarClientePFPorNome(String nome) {
		Query query = entityManager.createNamedQuery("PF.consultarPorNome");
		query.setParameter("nome", nome.toUpperCase() + "%");
		try {
			return query.getResultList();
		} catch (NoResultException e) {
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public List<PessoaFisica> consultarClientePFPorNomeAutoComplete(String nome) {
		Query query = entityManager.createNamedQuery("PF.consultarPorNomeAutoComplete");
		query.setParameter("nome", "%" + nome.toUpperCase() + "%");
		try {
			return query.getResultList();
		} catch (NoResultException e) {
			return null;
		}
	}

	public PessoaFisica consultarClientePFPorCpf(String cpf) {
		Query query = entityManager.createNamedQuery("PF.consultarPorCpf");
		query.setParameter("cpf", cpf);
		try {
			return (PessoaFisica) query.getSingleResult();
		} catch (NoResultException e) {
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public List<PessoaFisica> consultarClientePFPorCpfLista(String cpf) {
		Query query = entityManager.createNamedQuery("PF.consultarPorCpfLista");
		query.setParameter("cpf", cpf + "%");
		try {
			return query.getResultList();
		} catch (NoResultException e) {
			return null;
		}
	}
	
	public boolean verificarIdClientePF(long idCliente) {
		Query query = entityManager.createNamedQuery("PF.verificarIdCliente");
		query.setParameter("idCliente", idCliente);
		try {
			query.getSingleResult();
			return true;
		} catch (NoResultException e) {
			return false;
		}
	}
	public boolean verificarExisteCpf(String cpf, long idCliente) {
		Query query = entityManager.createNamedQuery("PF.verificarExisteCpf");
		query.setParameter("cpf", cpf);
		try {
			PessoaFisica pf = new PessoaFisica();
			pf = (PessoaFisica) query.getSingleResult();
			if (pf.getId() == idCliente) {
				return false;
			} else {
				return true;
			}
		} catch (NoResultException e) {
			return false;
		}
	}

}
